package cn.leaves.wb.db.dao.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import cn.leaves.wb.db.bean.Profile;
import cn.leaves.wb.db.dao.ProfileDAO;
@Service(value = "profileDAO")
public class ProfileDAOJDBCImpl extends BaseJDBC implements ProfileDAO {
	private Log log = LogFactory.getLog(getClass());

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.leaves.wb.db.dao.ProfileDAO#save()
	 */
	@Override
	public void save(Profile profile) throws SQLException {
		super.save(profile, new String[] { "modified" });
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.leaves.wb.db.dao.ProfileDAO#update()
	 */
	@Override
	public void update(Profile profile) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.leaves.wb.db.dao.ProfileDAO#delete(java.lang.Long)
	 */
	@Override
	public void delete(Long id) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.leaves.wb.db.dao.ProfileDAO#findById(java.lang.Long)
	 */
	@Override
	public Profile findById(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.leaves.wb.db.dao.ProfileDAO#findByNickName(java.lang.String)
	 */
	@Override
	public Profile findByNickName(String nickName) {
		String sql = "select * from profile where nickname=?";
		return jdbcTemplate.queryForObject(sql, new Object[] { nickName }, new ProfileRowMapper());
	}

	class ProfileRowMapper implements RowMapper<Profile> {

		@Override
		public Profile mapRow(ResultSet rs, int rowNum) throws SQLException {
			Profile profile = new Profile();
			profile.setId(rs.getLong("id"));
			profile.setNickname(rs.getString("nickname"));
			profile.setCreated(rs.getTimestamp("created"));
			profile.setModified(rs.getTimestamp("modified"));
			return profile;
		}

	}

}
